<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Session;
use think\Db;
use think\Validate;

class Agent extends Base
{
    /**
     * @param Request $request
     * @return \think\response\Json
     * 代理商创建
     */
    public function save(Request $request)
    {

        $data_user['user_name'] = $request->param('user_name') ?: 123;//代理商账户
        $password               = $request->param('password') ?: 123456;
        $data_user['password']  = md5(md5($password));//代理商密码
        $user_id                = Db::table('ad_user')->insertGetId($data_user);
        if ($user_id) {
            $data_user_info['uid']           = $user_id;
            $data_user_info['user_type']     = 3;
            $data_user_info['register_time'] = time();
            $data_user_info['invitation']    = get_random();
            $user_info_res                   = Db::table('ad_user_info')->insertGetId($data_user_info);
        }


        if ($user_info_res) {
            $all_time                             = $request->param('agent_time_str');
            $all_time_arr                         = explode(' - ', $all_time);
            $data_agent['secure_hand_id_card']    = $request->param('secure_hand_id_card') ?: '';//担保人手持身份证
            $data_agent['secure_front_id_card']   = $request->param('secure_front_id_card') ?: '';//担保人身份证正面
            $data_agent['secure_backend_id_card'] = $request->param('secure_backend_id_card') ?: '';//担保人身份证反面
            $data_agent['secure_name']            = $request->param('secure_name') ?: '';//担保人姓名
            $data_agent['secure_id_card_num']     = $request->param('secure_id_card_num') ?: '';//担保人身份证号码
            $data_agent['secure_mobile']          = $request->param('secure_mobile') ?: '';//担保人电话号
            $data_agent['secure_relation']        = $request->param('secure_relation') ?: '';//担保人关系
            $data_agent['uid']                    = $user_id;
            $data_agent['hand_id_card']           = $request->param('hand_id_card') ?: '';//代理商手持身份证
            $data_agent['front_id_card']          = $request->param('front_id_card') ?: '';//代理商身份证正面
            $data_agent['backend_id_card']        = $request->param('backend_id_card') ?: '';//代理商身份证反面
            $data_agent['linkman_mobile']         = $request->param('linkman_mobile') ?: '';//代理商有效联系人电话
            $data_agent['linkman']                = $request->param('linkman') ?: '';//代理商有效联系人
            $data_agent['act_start_time']         = strtotime($all_time_arr[0]);//代理开始时间
            $data_agent['act_end_time']           = strtotime($all_time_arr[1]);//代理结束时间
            $data_agent['address']                = $request->param('address') ?: '';//代理商地址
            $data_agent['id_card_num']            = $request->param('id_card_num') ?: '';//代理商身份证号码
            $data_agent['mobile']                 = $request->param('mobile') ?: '';//代理商电话
            $data_agent['city']                   = $request->param('city') ?: '';//代理商城市
            $data_agent['province']               = $request->param('province') ?: '';//代理商省份
            $data_agent['legal_person']           = $request->param('legal_person') ?: '';//代理商法人
            $data_agent['company_name']           = $request->param('company_name') ?: '';//代理商公司名称
            $data_agent['status']                 = $status = $request->param('status');//状态 1启用 0不启用
            $user_status                          = Db::table('ad_user_info')->where(['uid' => $user_id])->update(['status' => $status]);
            $data_agent['business_license_img']   = $request->param('business_license_img') ?: '';//代理商营业执照
            $data_agent['create_time']            = $data_agent['update_time'] = time();//代理商营业执照
            $user_agent_res                       = Db::table('ad_user_agent')->insertGetId($data_agent);
        }

        if ($user_agent_res) {
            if ($data_agent['act_end_time'] && $user_status) {
                //推入定时任务
                Db::table('ad_user_agent_crontab')->insertGetId([
                    'end_time' => $data_agent['act_end_time'],
                    'uid' => $user_id,
                    'create_time' => time(),
                ]);
            }
            $this->success('已创建');
        }
        $this->error('创建失败');
    }

    /**
     * @return \think\response\View
     * 添加页面
     */
    public function add()
    {
        $province = Db::table('ad_provinces')->select();
        $city     = Db::table('ad_cities')->select();
        return view('add', compact('province', 'city'));
    }

    /**
     * @param Request $request
     * 代理商列表
     */
    public function index(Request $request)
    {

        $page    = $request->param('page') ?: 1;
        $keyword = $request->param('keyword') ?: '';
        $limit   = $request->param('limit') ?: 10;
        if ($keyword) {
            $where['ad_user_agent.legal_person|ad_user_agent.company_name|ad_user_agent.mobile'] = [
                'like',
                "%{$keyword}%"
            ];
        }

        $data = Db::table('ad_user_agent')
            ->join('ad_user_info', 'ad_user_info.uid = ad_user_agent.uid', 'LEFT')
            ->where(['ad_user_info.user_type' => 3])->field(['ad_user_info.uid', 'ad_user_agent.*'])->paginate($limit, false, [
                'page' => $page,
                'query' => ['keyword' => $keyword]
            ]);

        return view('index', compact('data'));

    }

    public  function add_person(Request $request)
    {
        $province = $request->param('province');
        $city = $request->param('city');
        return view('add_person', compact('province', 'city'));
    }

    public function add_save(Request $request)
    {

        //var_dump($data_agent);exit();

        $data_user['user_name'] = $request->param('user_name') ?: 123;//代理商账户
        $password               = $request->param('password') ?: 123456;
        $data_user['password']  = md5(md5($password));//代理商密码
        $user_id                = Db::table('ad_user')->insertGetId($data_user);
        if ($user_id) {
            $data_user_info['uid']           = $user_id;
            $data_user_info['user_type']     = 2;
            $data_user_info['city']                   = $request->param('city') ?: '';//代理商城市
            $data_user_info['province']               = $request->param('province') ?: '';//代理商省份
            $data_user_info['register_time'] = time();
            $data_user_info['invitation']    = get_random();
            $user_info_res                   = Db::table('ad_user_info')->insertGetId($data_user_info);
        }


        if ($user_info_res) {
            $all_time                             = $request->param('agent_time_str');
            $all_time_arr                         = explode(' - ', $all_time);
            $data_agent['secure_hand_id_card']    = $request->param('secure_hand_id_card') ?: '';//担保人手持身份证
            $data_agent['secure_front_id_card']   = $request->param('secure_front_id_card') ?: '';//担保人身份证正面
            $data_agent['secure_backend_id_card'] = $request->param('secure_backend_id_card') ?: '';//担保人身份证反面
            $data_agent['secure_name']            = $request->param('secure_name') ?: '';//担保人姓名
            $data_agent['secure_id_card_num']     = $request->param('secure_id_card_num') ?: '';//担保人身份证号码
            $data_agent['secure_mobile']          = $request->param('secure_mobile') ?: '';//担保人电话号
            $data_agent['secure_relation']        = $request->param('secure_relation') ?: '';//担保人关系
            $data_agent['uid']                    = $user_id;
            $data_agent['hand_id_card']           = $request->param('hand_id_card') ?: '';//代理商手持身份证
            $data_agent['front_id_card']          = $request->param('front_id_card') ?: '';//代理商身份证正面
            $data_agent['backend_id_card']        = $request->param('backend_id_card') ?: '';//代理商身份证反面
            $data_agent['linkman_mobile']         = $request->param('linkman_mobile') ?: '';//代理商有效联系人电话
            $data_agent['linkman']                = $request->param('linkman') ?: '';//代理商有效联系人
            $data_agent['act_start_time']         = strtotime($all_time_arr[0]);//代理开始时间
            $data_agent['act_end_time']           = strtotime($all_time_arr[1]);//代理结束时间
            $data_agent['address']                = $request->param('address') ?: '';//代理商地址
            $data_agent['id_card_num']            = $request->param('id_card_num') ?: '';//代理商身份证号码
            $data_agent['mobile']                 = $request->param('mobile') ?: '';//代理商电话
            $data_agent['city']                   = $request->param('city') ?: '';//代理商城市
            $data_agent['province']               = $request->param('province') ?: '';//代理商省份
            $data_agent['legal_person']           = $request->param('legal_person') ?: '';//代理商法人
            $data_agent['company_name']           = $request->param('company_name') ?: '';//代理商公司名称
            $data_agent['status']                 = $status = $request->param('status');//状态 1启用 0不启用
            $user_status                          = Db::table('ad_user_info')->where(['uid' => $user_id])->update(['status' => $status]);
            $data_agent['business_license_img']   = $request->param('business_license_img') ?: '';//代理商营业执照
            $data_agent['create_time']            = $data_agent['update_time'] = time();//代理商营业执照
            $user_agent_res                       = Db::table('ad_user_agent')->insertGetId($data_agent);
        }

        if ($user_agent_res) {
            if ($data_agent['act_end_time'] && $user_status) {
                //推入定时任务
                Db::table('ad_user_agent_crontab')->insertGetId([
                    'end_time' => $data_agent['act_end_time'],
                    'uid' => $user_id,
                    'create_time' => time(),
                ]);
            }
            $this->success('已创建');
        }
        $this->error('创建失败');
    }

    public function person_agent(Request $request)
    {
        $page       = $request->param('page') ?: 1;
        $limit      = $request->param('limit') ?: 10;
        $id = $request->param('id');
       $info =  Db::table('ad_user_info')->find($id);
        $city = $info['city'];//var_dump($city);exit();
        $province =$info['province'];
        $user = Db::table('ad_user_info')
                  ->alias('ui')
                  ->where(['ui.city' => $city,'ui.province' => $province])
                  ->field('ui.uid,ui.nick_name ,( SELECT NAME FROM ad_industry WHERE id = job_three) job , (SELECT user_name FROM ad_user WHERE id = uid) phone , 
                  ( SELECT province FROM ad_provinces WHERE provinceid = ui.province) AS province ,
                  ( SELECT city FROM ad_cities WHERE cityid = ui.city) AS city,
                  ( SELECT sum(money) FROM ad_user_money WHERE uid = ui.uid) touru_money ,ui.register_time')
                  ->paginate($limit, false, ['page' => $page]);


        return view('info', compact('user','invitation','province','city'));
    }

    /**
     * @param Request $request
     * 编辑
     */
    public function edit(Request $request)
    {
        $id               = $request->param('id');
        $data             = Db::table('ad_user_info')
            ->join('ad_user_agent', 'ad_user_agent.uid = ad_user_info.uid', 'LEFT')
            ->where(['user_type' => 3, 'ad_user_info.uid' => $id])->field([
                'ad_user_info.uid',
                'ad_user_info.status',
                'ad_user_info.invitation',
                'ad_user_agent.*',
            ])->find();

        $data['all_time'] = date('Y-m-d H:i:s', $data['act_start_time']) . ' - ' . date('Y-m-d H:i:s',
                $data['act_end_time']);
        //省份搜索数据
        $province = Db::table('ad_provinces')->select();
        $city     = Db::table('ad_cities')->select();
        return view('edit', compact('data', 'province', 'city'));
    }

    /**
     * @param Request $request
     * 更新
     */
    public function update(Request $request)
    {

        $uid                   = $request->param('uid');
        $password              = $request->param('password') ?: '';
        $data_user['password'] = md5(md5($password));//代理商密码
        if ($password) {
            Db::table('ad_user')->where(['id' => $uid])->update(['password' => $data_user['password']]);
        }

        $all_time                             = $request->param('agent_time_str');
        $all_time_arr                         = explode(' - ', $all_time);
        $data_agent['secure_hand_id_card']    = $request->param('secure_hand_id_card') ?: '';//担保人手持身份证
        $data_agent['secure_front_id_card']   = $request->param('secure_front_id_card') ?: '';//担保人身份证正面
        $data_agent['secure_backend_id_card'] = $request->param('secure_backend_id_card') ?: '';//担保人身份证反面
        $data_agent['secure_name']            = $request->param('secure_name') ?: '';//担保人姓名
        $data_agent['secure_id_card_num']     = $request->param('secure_id_card_num') ?: '';//担保人身份证号码
        $data_agent['secure_mobile']          = $request->param('secure_mobile') ?: '';//担保人电话号
        $data_agent['secure_relation']        = $request->param('secure_relation') ?: '';//担保人关系
        $data_agent['hand_id_card']           = $request->param('hand_id_card') ?: '';//代理商手持身份证
        $data_agent['front_id_card']          = $request->param('front_id_card') ?: '';//代理商身份证正面
        $data_agent['backend_id_card']        = $request->param('backend_id_card') ?: '';//代理商身份证反面
        $data_agent['linkman_mobile']         = $request->param('linkman_mobile') ?: '';//代理商有效联系人电话
        $data_agent['linkman']                = $request->param('linkman') ?: '';//代理商有效联系人
        $data_agent['act_start_time']         = strtotime($all_time_arr[0]);//代理开始时间
        $data_agent['act_end_time']           = strtotime($all_time_arr[1]);//代理结束时间
        $data_agent['address']                = $request->param('address') ?: '';//代理商地址
        $data_agent['id_card_num']            = $request->param('id_card_num') ?: '';//代理商身份证号码
        $data_agent['mobile']                 = $request->param('mobile') ?: '';//代理商电话
        $data_agent['city']                   = $request->param('city') ?: '';//代理商城市
        $data_agent['province']               = $request->param('province') ?: '';//代理商省份
        $data_agent['legal_person']           = $request->param('legal_person') ?: '';//代理商法人
        $data_agent['company_name']           = $request->param('company_name') ?: '';//代理商公司名称
        $data_agent['business_license_img']   = $request->param('business_license_img') ?: '';//代理商营业执照
        $data_agent['status']                 = $status = $request->param('status');//状态 1启用 0不启用
        $data_agent['create_time']            = $data_agent['update_time'] = time();//代理商营业执照
        $user_status                          = Db::table('ad_user_info')->where(['uid' => $uid])->update(['status' => $status]);
        $user_agent_res                       = Db::table('ad_user_agent')->where(['uid' => $uid])->update($data_agent);

        if ($user_agent_res || $user_status) {
            //定时任务更新
            Db::table('ad_user_agent_crontab')->where(['uid' => $uid])->update(['end_time' => $data_agent['act_end_time']]);
            $this->success('已更新');
        }
        $this->error('更新失败');
    }
}